Yacon.widget.grid.list.ListHead = function (conText)
{
	this.parentList       = null;
	this.parentDiv        = null;
	this.parentTable      = null;
	this.visibleCount     = 0;
	
	Yacon.apply(this,conText);
	this.create();	
}

Yacon.widget.grid.list.ListHead.Extend(Yacon.widget.grid.list.BaseRow,{
	
	create:function()
	{
		this.fieldNames       =  new Array();
		this.fieldList        =  new Array();
		
		if(Yacon.isBootstrap()){
			
			this.parentTable = $("#"+this.parentList.elementID+" table");
			
			this.element = this.parentTable.find("thead");
			this.element.Control = this;
						
			var headSize = this.element.find("td").size();
			for(var i=0;i<headSize;i++){
				
				var field = this.element.find("td:eq("+i+")");
							
				var fieldName = field.attr("fieldname");
				var fieldText = field.attr("fieldtext");
				var isDisplay = field.attr("fieldvisible");
				
				if(fieldName != undefined){
					
					var headCell = new Yacon.widget.grid.list.HeadCell({element:field});
					headCell.parent = this;
					headCell.cellIndex = i;
					if(headCell.isFieldVisible()){
						this.visibleCount ++;
					}
					
					this.addField(headCell);
					
				}
				
			}
			
		}else{
			
			//表头
			this.parentDiv            	= document.all["divhead_" + this.parentList.elementID];
			this.parentTable           	= document.all["tabhead_" + this.parentList.elementID];
			
			//无表头
			if (!this.parentTable.rows || this.parentTable.rows.length==0)
				return;
			
			this.element         = this.parentTable.rows[0];
			this.element.Control = this;
			
			var headCell;
			for (var i=0;i< this.element.cells.length;i++)
			{
				headCell = new Yacon.widget.grid.list.HeadCell({element:this.element.cells[i]});
				headCell.parent = this;
				headCell.cellIndex =i;
				if (headCell.isFieldVisible())
					this.visibleCount ++;
				
				this.addField(headCell);
			}
			
		}
		
				
	},
	convertDataset:function()
	{
		var dataset = this.parentList.getDataSet();		
		if (dataset.getFieldCount()==0)
		{
			for (var i=0; i<this.getFieldCount(); i++)
			{
				if (this.getField(i) != null)
				{
					dataset.createTitleField(this.activeField.getFieldName());
				}
			}
		}
	},
	getVisibleCount:function()
	{		
		return this.visibleCount;
	},
	setWidth:function(width)
	{
		if(Yacon.getInt(width)>0)
			this.parentDiv.style.width 	= width+"px";
	},
	getWidth:function()
	{
		return this.parentTable.offsetWidth;
	},
	getCellWidth:function(cellInx)
	{
		return this.element.cells[cellInx].offsetWidth;
	},
	setScrollLeft:function(scrollLeft)
	{
		this.parentDiv.scrollLeft = scrollLeft;
	},
	setSpaceWidth:function(spaceWidth)
	{
		for(var i=0;i< this.getFieldCount();i++)
		{
			if (this.getField(i) != null && this.activeField.isFieldVisible())
			{
				this.activeField.setWidth(this.activeField.getWidth()+spaceWidth);
			}
		}
	}
});



Yacon.widget.grid.list.ListView = function (conText)
{
	this.oddRowClass  = "tableOddRow";
	this.evenRowClass = "tableEvenRow";	
	this.recordCount  = 0;
	this.pageCount    = 0;
	this.activeRow    = null;	
	this.rowList      = null;
	this.isScroll     = false;
	
	Yacon.apply(this,conText);
	
	this.create();	
};

Yacon.widget.grid.list.ListView.Extend(Yacon.Component,{
		
	create:function()
	{
		this.elementDiv   = document.all["divview_" + this.parentList.elementID];
		this.element      = document.all["tabview_" + this.parentList.elementID];
		
		this.elementDiv.Control = this;
		this.element.Control = this;
		
		this.rowList    = new Array();
	},
	creatRow:function()
	{
		var DataRow        = new Yacon.widget.grid.list.DataRow();
		DataRow.parent     = this;
		DataRow.parentList = this.parentList;
		DataRow.record     = this.parentList.getDataSet().getRecord();
		
		DataRow.creatElement();
	},
	setWidth:function(width)
	{
		var offWidth = 0;
		if(Yacon.getInt(width)>0)
		{
			this.isScroll     = false;
			
			this.elementDiv.style.width = width +"px";
			offWidth = this.elementDiv.offsetWidth;
			
			if (this.elementDiv.offsetHeight < this.element.offsetHeight)
			{
				offWidth = offWidth - 20 ;
				this.isScroll = true;
			}
		}		
		return offWidth;
	},
	getWidth:function()
	{
		return this.element.offsetWidth;
	},
	changeFieldWidth:function(spaceWidth)
	{
	
		for(var j=0;j< this.element.rows.length;j++)
		{
			var oRow = this.element.rows[j];	
			for (var i = 1; i < oRow.cells.length; i++)
			{
				var width = this.parentList.headControl.getCellWidth(i);
				if (window.isChrome||window.isfirefox)
					width = width-2;
				oRow.cells[i].style.width = width+"px";				
			}
			if(j>1){
				break ;
			}
		}		
	},
	setHeight:function(gridHeight)
	{
		 this.elementDiv.style.height = gridHeight+"px";
	},
	getHeight:function()
	{
		return this.element.offsetHeight;
	},
	getAbsTop:function()
	{
		return this.getAbsPos(this.elementDiv)[1];
	},
	deleteRow:function()
	{
		var checkflag = false;
		if (this.parentList.isCheck())
		{
			var curRow; 
			for(var i= this.element.rows.length-1;i>-1;i--)
			{
				curRow = this.element.rows[i];
				if (curRow.Control.isCheck())
				{
					curRow.Control.convertRecord();
					this.parentList.getDataSet().deleteRecord(curRow.Control.getRecord());
					checkflag = true;
				}
			}			
		}else
		{
			if (this.activeRow && this.activeRow.Control)
			{
				this.parentList.getDataSet().deleteRecord(this.activeRow.Control.getRecord());
				checkflag = true;
			}
		}
		if (!checkflag)
			alert("请选择删除记录！");
		
		return checkflag;
	},
	deleteRowByDataSet:function()
	{
		var dataSet   =  createDataset("");	
		
		dataSet.setExcuteMethod(this.parentList.getDataSet().excuteMethod); 
		var datause = false ;
		if (this.parentList.isCheck())
		{
			var curRow; 
			for(var i= this.element.rows.length-1;i>-1;i--)
			{
				curRow = this.element.rows[i];
				if (curRow.Control.isCheck())
				{
					curRow.Control.convertRecord();
					var record = curRow.Control.getRecord();
					if (record.getState() == "insert")
					{
						this.parentList.getDataSet().cancelPriRecord(record);
					}else
					{
						record.setState("delete");
						dataSet.put(record);
						datause = datause || true ; 
					}
				}
			}			
		}else
		{
			if (this.activeRow && this.activeRow.Control)
			{
				var record = this.activeRow.Control.getRecord();
				if (record.getState() == "insert")
				{
					this.parentList.getDataSet().cancelPriRecord(record);
				}else
				{
					record.setState("delete");
					dataSet.put(record);
					datause = datause || true ; 
				}
			}
		}
		if(!datause){
			return null ;
		}
		return dataSet;
	},
	deleteViewRow:function(record)
	{
		var delRow  = this.activeRow;
		
		if (record)
			delRow = this.getRowByRecord(record);
		
		if (delRow)
		{
			delRow.parentNode.removeChild(delRow);
		}
	},
	broadRow:function(record)
	{
		var broadRow  = this.activeRow;
		if (record)
			broadRow = this.getRowByRecord(record);
		
		if (broadRow.Control)
			broadRow.Control.broadRecord();
	},
	editRow:function(record)
	{
		this.setRowState("edit");
		if (this.activeRow && this.activeRow.Control)
			this.activeRow.Control.broadDetailPage();
	},
	getRowByRecord:function(record)
	{
		if (!record)
			return;
		
		for (var i = 0; i < this.element.rows.length; i++)
		{
			var oRow = this.element.rows[i];
			if (oRow.Control && oRow.Control.getRecord()== record)
			{
				return oRow;
			}
		}		
	},
	setFieldValue:function(fieldName,fieldValue,fieldText)
	{
		if (this.activeRow && this.activeRow.Control)
			this.activeRow.Control.setFieldValue(fieldName,fieldValue,fieldText);
	},
	getRecordCount:function()
	{
		this.recordCount = Yacon.getInt(this.element.getAttribute("recordCount"));
		
		return this.recordCount;		
	},
	setRecordCount:function(recordCount)
	{
		this.recordCount = recordCount;		
		this.element.setAttribute("recordCount",recordCount);
	},
	validateCheck:function(){
		if(this.recordCount==0){
			var checkObj = document.all("title_choice_"+this.parentList.elementID);
			if(checkObj!=null){
				checkObj.checked = false ;
			}
		}
	},
	getPageCount:function()
	{
		this.pageCount = Yacon.getInt(this.element.getAttribute("pageCount"));
		
		return this.pageCount;			
	},
	setPageCount:function(pageCount)
	{
		this.pageCount = pageCount;		
		this.element.setAttribute("pageCount",pageCount);		
	},
	
	changeRowColor :function ()
	{
		
		for (var i = 0; i < this.element.rows.length; i++)
		{
			var oRow = this.element.rows[i];			
			if ((i % 2) == 0)
			{	
				oRow.className = this.oddRowClass; 				
			}else{				
				oRow.className = this.evenRowClass; 
			}
		}	
	},
	setActiveRow :function (rowobj)
	{	
		
		if (rowobj == undefined ||this.activeRow == rowobj)
			return;
				
		if(this.activeRow != undefined)
		{
			this.activeRow.className = this.activeRow.getAttribute("oldClassName");				
		}
		this.activeRow = rowobj;
		this.activeRow.setAttribute("oldClassName",this.activeRow.className); 
		this.activeRow.className= "rowSelected";
	
		if (this.parentList.getDataSet())
		{
			this.activeRow.Control.convertRecord();
			this.parentList.getDataSet().getRecord(this.activeRow.Control.getRecord());
			this.setRowState(this.parentList.getDataSet().getState());
		}
	},
	creatElementTemplate:function()
	{
		
		var tableStr = "<table id='tabview_"+ this.parentList.elementID +"' cellspacing='1' 'border='0' cellpadding='1'>";		
		var dataset = this.parentList.getDataSet();		
		var rowIndex = 0;
		
		while(dataset.next())
		{
			DataRow            = new Yacon.widget.grid.list.DataRow();
			DataRow.parent     = this;
			DataRow.parentList = this.parentList;
			DataRow.record     = dataset.getRecord();			
			tableStr = tableStr + DataRow.creatElementTemplate(rowIndex);			
			rowIndex ++;
			this.rowList[this.rowList.length] = DataRow;	
		}
		
		this.elementDiv.innerHTML = tableStr + "</table>";		
		this.element      = document.all["tabview_" + this.parentList.elementID];
		if (this.element)
		{
			this.element.Control = this;
		
			for (var i = 0; i < this.rowList.length; i++)
			{
				this.rowList[i].setElement(this.element.rows[i]);
			}
			
			if (this.element.rows.length >0)
			{
				this.setActiveRow(this.element.rows[0]);
			}
		}
		
	},
	setElementTemplate:function(tableHtml)
	{
		this.elementDiv.innerHTML = tableHtml;		
		this.element      = document.all["tabview_" + this.parentList.elementID];
		
		this.convertDataset();
		
		this.element      = document.all["tabview_" + this.parentList.elementID];
		if (this.element)
			this.element.Control = this;
		
	},
	
	convertDataset:function()
	{
		var dataset = this.parentList.getDataSet();	
		var DataRow;
		for (var i = 0; i < this.element.rows.length; i++)
		{
			var oRow           = this.element.rows[i];
			DataRow            = new Yacon.widget.grid.list.DataRow({element:oRow});
			DataRow.parent     = this;
			DataRow.parentList = this.parentList;
			DataRow.record     = dataset.createPureRecord("past");
			DataRow.convertRecord();
		}

		if (this.element.rows.length >0)
			this.setActiveRow(this.element.rows[0]);
	},
	setRowState:function(state)
	{
		if (this.activeRow)
		{
			switch (state.toLowerCase())
			{
				case "insert":
				case "edit":
				case "delete":
				{
					this.activeRow.setAttribute("state",state.toLowerCase());
					break;
				}
				default :
				{
					this.activeRow.setAttribute("state","past");
					this.activeRow.style.textDecorationLineThrough=false;
				    break;	
				}
			}			
		}
	},
	getCheckDataSet:function()
	{
		var checkDataSet =  new Yacon.data.DataSet();
		if (this.parentList.isCheck())
		{
			for (var i = 0; i < this.element.rows.length; i++)
			{
				curRow = this.element.rows[i];
				if (curRow.cells[0].children[0]&& curRow.cells[0].children[0].checked)
				{
					checkDataSet.insert("end",null,curRow.Control.record);
				}				
			}
			
			checkDataSet.beforeFirst();
		}
		return checkDataSet;		
	},
	
	free: function()
	{
		for(var i= this.element.rows.length-1;i>-1;i--)
		{
			this.element.rows[i].Control.free();
			if (window.isIE)
				this.element.rows[i].removeNode(true);
		}
		if (!window.isIE)
			this.element.innerHTML="";
		
		this.rowList = new Array();
		this.activeRow = null;
	}
	
});


Yacon.widget.grid.list.ListView.events = {
		
	onGridScroll:function(grid)
	{
		var element;
		if (grid)
			element = grid;
		else				
			element = event.srcElement;
		
		if (element.Control && element.Control.parentList)
			element.Control.parentList.headControl.setScrollLeft(element.scrollLeft);
	}
};




